import type { AppRouteModule } from '@/router/types';
import { LAYOUT } from '@/router/constant';
import { PermissionEnum } from '@/enums/permissionEnum'
import { GetPartnerListParams } from '@/api/model/partner';
import { getPartnerList } from '@/api/partner';
import { partnerTypeEnum } from '@/enums/partner';

function fetchApps() {
  var children = [
    {
      path: 'index',
      name: 'index',
      component: () => import('@/views/wex/app/index.vue'),
      meta: {
        title: '应用管理',
      },
    },
    {
      path: ':id',
      name: 'app',
      component: () => import('@/views/wex/app/AppPage.vue'),
      meta: {
        title: '应用',
        hideMenu: true,
      },
    },
  ];
  const params: GetPartnerListParams = {
    type: partnerTypeEnum.APP,
  }
  getPartnerList(params, 'none').then(
    (res) => {
      const items = res.items;
      for (const item of items) {
        let childRoute = {
          path: `/app/${item.id}`,
          meta: {
            title: item.name,
            ignoreRoute: true,
          },
        };
        children.push(childRoute);
      }
    }
  );
  return children;
}

const route: AppRouteModule = {
  path: '/app',
  name: 'app',
  component: LAYOUT,
  redirect: '/app',
  meta: {
    orderNo: 6000,
    icon: 'ant-design:appstore-add-outlined',
    title: '应用管理',
    permissions: [PermissionEnum.APP_MANAGE],
  },
  children: fetchApps(),
};

export default route;